//
//  MyCommentViewController.m
//  ChunLaiZhuangClient
//
//  Created by zzs on 16/2/25.
//  Copyright © 2016年 CLZ. All rights reserved.
//

#import "MyCommentViewController.h"
#import "CustomCommentTableViewCell.h"
#import "CommentListModel.h"
#import "CLZRefreshHeader.h"
#import "CLZRefreshFooter.h"

@interface MyCommentViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, strong) NSMutableArray *commentListArray;
@property(nonatomic,assign) NSInteger pageNum;
@property(nonatomic,copy) NSString *productId;
@end

@implementation MyCommentViewController

- (id)initWithProductId:(NSString *)productId {
    if (self = [super init]) {
        self.productId = productId;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.pageNum = 1;
    self.commentListArray = [NSMutableArray array];
    [self setNaviTitle:@"我的点评"];
    if (self.productId.length > 0) {
        [self setNaviTitle:@"商品点评"];
    }
    [self setNaviLeftButton:@""];
    [self.view addSubview:self.displayView];
    [self loadDataCommentList:1];
    // Do any additional setup after loading the view.
}
#pragma mark-加载数据
-(void)loadDataCommentList:(NSInteger)pageNum{
    self.pageNum = pageNum;
    __weak typeof(self) weakSelf = self;
    if (self.productId.length <= 0) {
        [ApiClient getHaveCommentListPageNumber:[NSString stringWithFormat:@"%ld", pageNum] didFinishLoaded:^(CommentListModel *model, NSString *path) {
            [weakSelf loadDataSuccess:model pageNum:pageNum];
        } didFailLoaded:^(NSError *error, NSString *path) {
            [weakSelf.displayView.mj_header endRefreshing];
            [weakSelf.displayView.mj_footer endRefreshing];
        }];
    } else {
        [ApiClient getProductCommentList:self.productId pageNumber:pageNum didFinishLoaded:^(CommentListModel *model, NSString *path) {
            [weakSelf loadDataSuccess:model pageNum:pageNum];
        } didFailLoaded:^(NSError *error, NSString *path) {
            [weakSelf.displayView.mj_header endRefreshing];
            [weakSelf.displayView.mj_footer endRefreshing];
        }];
    }
}

- (void)loadDataSuccess:(CommentListModel *)model pageNum:(NSInteger)pageNum{
    [self.displayView.mj_header endRefreshing];
    if (model.data.total_page.integerValue == self.pageNum) {
        [self.displayView.mj_footer endRefreshingWithNoMoreData];
    } else {
        [self.displayView.mj_footer endRefreshing];
    }
    if ([model.status intValue] == 1) {
        if (pageNum == 1) {
            [self.commentListArray removeAllObjects];
        }
        if (model.data.reviews.count > 0) {
            [self.commentListArray addObjectsFromArray:model.data.reviews];
        }
        if (![self.view.subviews containsObject:self.displayView]) {
            [self.view addSubview:self.displayView];
        }else{
            [self.displayView reloadData];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(UITableView*)displayView{
    if (!_displayView) {
        _displayView=[[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
        _displayView.delegate=self;
        _displayView.dataSource=self;
        _displayView.backgroundColor=[UIColor clearColor];
        _displayView.tableFooterView=[[UIView alloc] initWithFrame:CGRectZero];
        __weak typeof(self) weakSelf = self;
        _displayView.mj_header = [CLZRefreshHeader headerWithRefreshingBlock:^{
            [weakSelf loadDataCommentList:1];
        }];
        _displayView.mj_footer = [CLZRefreshFooter footerWithRefreshingBlock:^{
            [weakSelf loadDataCommentList:++self.pageNum];
        }];
        // [_displayView setSeparatorColor:[UIColor redColor]];
    }
    return _displayView;
}
#pragma mark-UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.commentListArray.count==0) {
        return 1;
    }else{
        return self.commentListArray.count;
    }
   
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.commentListArray.count==0) {
            QLKEmptyCellTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:kEmptyCellTableViewCellIdentify];
            if (!cell) {
                cell = [[QLKEmptyCellTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kEmptyCellTableViewCellIdentify];
            }
            [cell setLogoImage: @"sign_pinglun" cellHeight:self.displayView.height];
            cell.titleLabel.text = @"暂无评论" ;
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            return cell;
    }else{
        static NSString *identifier = @"CustomCommentTableViewCell";
        CustomCommentTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:
                                            identifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomCommentTableViewCell"  owner:self options:nil] lastObject];
            
        }
        CommentModel *commentModel=self.commentListArray[indexPath.row];
        [cell setCommentModel:commentModel];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
        return cell;

    }

   
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.commentListArray.count==0) {
          return self.displayView.height;
    }else{
       return 100;
    }

    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
